package app.sceledroid.activity.course;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import app.sceledroid.R;
import app.sceledroid.activity.CourseActivity;
import app.sceledroid.activity.MainActivity;
import app.sceledroid.adapter.CourseAdapter;
import app.sceledroid.adapter.DeadlineAdapter;
import app.sceledroid.entity.Course;
import app.sceledroid.entity.Deadline;
import app.sceledroid.filechooser.FileChooser;
import app.sceledroid.services.SceleService;
import app.sceledroid.services.SceleService.SceleBinder;

public class DeadlineActivity extends Activity {

	private SceleService sceleService;
	private boolean isBounded;
	private ArrayList<Deadline> deadlines;
	private DeadlineAdapter deadlineAdapter;
	// private ListView courseListView;
	private AlertDialog alertDialog; 
	private ProgressDialog progressDialog;
	private String courseName;
	private ServiceConnection sceleServiceConnection = new ServiceConnection() {

		//@Override
		public void onServiceConnected(ComponentName componentName,
				IBinder iBinder) {
			// TODO Auto-generated method stub
			SceleBinder binder = (SceleBinder) iBinder;
			sceleService = binder.getService();
			isBounded = true;
		}

		//@Override
		public void onServiceDisconnected(ComponentName componentName) {
			// TODO Auto-generated method stub
			isBounded = false;
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.course_deadline_view);
		
		
	
		deadlines = new ArrayList<Deadline>();
		deadlineAdapter = new DeadlineAdapter(DeadlineActivity.this,
				R.id.list_no_item, deadlines);
		Intent service = new Intent(this, SceleService.class);
		startService(service);

		Bundle bundle = getIntent().getExtras();
		courseName = bundle.getString("CourseName");

		final Button fetchButton = (Button) findViewById(R.id.fetchDeadline);
		ListView deadlineListView = (ListView) findViewById(R.id.deadlineList);
		deadlineListView.setOnItemClickListener(new OnItemClickListener() {

			//@Override
			public void onItemClick(AdapterView adapterView, View view,
					int index, long position) {
				// TODO Auto-generated method stub
				// Intent intent = new Intent(DeadlineActivity.this,
				// CourseActivity.class);

				Deadline d = deadlineAdapter.getItem(index);
				
				//new Intent(getParent().getApplicationContext() , FileChooser.class);
				//Intent filechooser = new Intent(DeadlineActivity.this , FileChooser.class);
				Intent filechooser = new Intent(DeadlineActivity.this , FileChooser.class);
				Bundle extras = new Bundle();
				extras.putString("durl", d.getUrl());
				extras.putString("dname", d.getName());
				extras.putString("dstart", d.getDateStart().toString());
				extras.putString("dend", d.getDateEnd().toString());
				extras.putString("dinfo", d.getInfo());
				filechooser.putExtras(extras);
				
				startActivity(filechooser);
				Toast.makeText(DeadlineActivity.this, d.getName() + d.getUrl(),
						Toast.LENGTH_LONG);
			}
		});
		deadlineListView.setAdapter(deadlineAdapter);

		fetchButton.setOnClickListener(new OnClickListener() {

			//@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(MainActivity.sceleService != null)
				{
					Toast.makeText(DeadlineActivity.this, "Scele Service Not null", Toast.LENGTH_LONG);
				}
				
				if (sceleService != null) {
					Toast.makeText(DeadlineActivity.this,
							"Scele Service Not NULL", Toast.LENGTH_LONG).show();

					ArrayList<Deadline> temp = sceleService
							.getDeadline(courseName);
					
					if (temp != null) {
						deadlines.addAll(temp);
						for (Deadline d : deadlines) {
							Log.v("Deadline : ", d.getName() + d.getUrl());
						}
						deadlineAdapter.notifyDataSetChanged();
						// listView.

						fetchButton.setVisibility(View.GONE);
					}
					else
					{
						Toast.makeText(DeadlineActivity.this,
								"Deadlines is NULL", Toast.LENGTH_LONG).show();
					}
				} else {
					
					Toast.makeText(DeadlineActivity.this, "Scele Service NULL",
							Toast.LENGTH_LONG).show();
					bindToService();
				}
			}
		});

	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

		bindToService();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	private void bindToService() {
		Intent service = new Intent(this, SceleService.class);
		getParent().bindService(service, sceleServiceConnection, Context.BIND_AUTO_CREATE);

	}

}
